"""
记录一些通用方法
基础方法，请测试效率，少用递推式简写
"""

def compare_object(obj1, obj2):
    """
    比较两个同类对象内容，返回内容不同的属性
    @param:
        obj1: 对象1
        obj2: 对象2
    """
    list = []
    if type(obj1) != type(obj2):
        print('can not compare different type object')
        return list
    for attr in dir(obj1):
        if attr.startswith('__'):
            continue
        if getattr(obj1, attr) != getattr(obj2, attr):
            list.append(attr)
    return list


# 经测试，这个效率太差了
# def compare_object_list(obj_list1, obj_list2):
#     """
#     比较两个对象列表，返回差集和交集
#     @param:
#         obj_list1: 列表1
#         obj_list2: 列表2
#     """
#     list1 = [obj.__dict__ for obj in obj_list1]
#     list2 = [obj.__dict__ for obj in obj_list2]

#     only_in_list1 = [obj for obj in obj_list1 if obj.__dict__ not in list2]
#     only_in_list2 = [obj for obj in obj_list2 if obj.__dict__ not in list1]
#     both_in = [obj for obj in obj_list1 if obj.__dict__ in list2]
#     return only_in_list1, only_in_list2, both_in


def compare_object_list_by_attr(obj_list1, obj_list2, attr):
    """
    根据pk比较两个对象列表，返回差集和交集
    @param:
        obj_list1: 列表1
        obj_list2: 列表2
    @return:
        only_in_list1: 只在列表1中的对象
        only_in_list2: 只在列表2中的对象
        both_in: 在两个列表中的对象
    """
    if not obj_list1 or not obj_list2:
        return [], [], []
    dict1 = {getattr(obj, attr): obj for obj in obj_list1}
    dict2 = {getattr(obj, attr): obj for obj in obj_list2}
    key_in_dict1, key_in_dict2, key_in_both = compare_list(list(dict1.keys()), list(dict2.keys()))
    both = [dict1[item] for item in key_in_both]
    only_in_list1 = [dict1[item] for item in key_in_dict1]
    only_in_list2 = [dict2[item] for item in key_in_dict2]
    return only_in_list1, only_in_list2, both


def compare_objects(obj_list1, obj_list2):
    """
    比较两个对象列表，返回差集和交集
    @param:
        obj_list1: 列表1
        obj_list2: 列表2
    @return:
        only_in_list1: 只在列表1中的对象
        only_in_list2: 只在列表2中的对象
        both_in: 在两个列表中的对象
    """
    if not obj_list1 or not obj_list2:
        return [], [], []
    dict1 = {str(obj.__dict__): obj for obj in obj_list1}
    dict2 = {str(obj.__dict__): obj for obj in obj_list2}
    key_in_dict1, key_in_dict2, key_in_both = compare_list(list(dict1.keys()), list(dict2.keys()))
    both = [dict1[item] for item in key_in_both]
    only_in_list1 = [dict1[item] for item in key_in_dict1]
    only_in_list2 = [dict2[item] for item in key_in_dict2]
    return only_in_list1, only_in_list2, both


def compare_list(list1, list2):
    """
    比较两个列表，返回差集和交集
    """
    if not list1 or not list2:
        return [], [], []
    in_list1 = list(set(list1) - set(list2))
    in_list2 = list(set(list2) - set(list1))
    both_in = list(set(list1) & set(list2))
    return in_list1, in_list2, both_in


def convert_equlexp_to_dict(equlexp):
    """
    将等式字符串转换成字典
    exp: seconds = 3,  2, hours = 1
    return: {'seconds': 3, 'hours': 1}
    """
    if not equlexp:
        return None
    result = {}
    for item in equlexp.replace(' ', '').split(','):
        if '=' in item:
            key, value = item.split('=')
            if value.isdigit():
                value = int(value)
            result[key] = value
    return result

def convert_cron_to_dict(cron):
    """
    将字符串转换成字典
    """
    if not cron:
        return None
    result = {}
    for item in cron.split(' '):
        if '=' in item:
            key, value = item.split('=')
            if value.isdigit():
                value = int(value)
            result[key] = value
    return result


if __name__ == '__main__':
    equlexp = 'seconds = 3,  2, hours = 1'
    print(convert_equlexp_to_dict(equlexp))

    class user():
        def __init__(self, name, age):
            self.name = name
            self.age = age

    a = user('a', 1)
    b = user('a', 2)
    print(compare_object(a, b))
